<?php
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'ProjectConfiguration.class.php');

class namespaceMytaskTask extends sfBaseTask
{
	const filename = 'sitemap.xml';
	const iplife = 'http://iplife.com.ua';
	public $webdir = '';
	public $html = "";

	protected function configure()
	{

		$this->addOptions(array(
		new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'frontend'),
		new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'false'),
		new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
		/*		new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'frontend'),
		 new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
		 new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),*/
		// add your own options here ру
		));

		$this->namespace        = 'namespace';
		$this->name             = 'mytask';
		$this->briefDescription = '';
		$this->detailedDescription = '';
	}

	protected function execute($arguments = array(), $options = array())
	{

		// initialize the database connection
		$databaseManager = new sfDatabaseManager($this->configuration);
		$connection = $databaseManager->getDatabase($options['connection'])->getConnection();

		$this->webdir = sfConfig::get('sf_web_dir');
		$outel = new SimpleXMLElement('<urlset></urlset>', NULL, false); // THE MAIN ELEMENT
		$this->generatePageXml($outel->addChild('url',''), 'http://iplife.com.ua', date("Y-m-d", time()), 'daily', 1);

		$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', 'false');
		sfContext::createInstance($configuration);
		sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url', 'Asset', 'Tag'));

		$outel->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

		//ADD NEWS - Main Page AND ALL
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('news/index')), date("Y-m-d", time()), 'daily', 0.5);
		$news = Doctrine::getTable('News')->findAll();
		foreach($news as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('news_view', array('alias'=>$item['alias']))), substr($item['updated_at'], 0, 10), 'yearly', 1);
			
		//ADD ARTICLES - Main Page AND ALL
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('article/index')), date("Y-m-d", time()), 'daily', 0.5);

		$con = Doctrine_Manager::getInstance()->connection();
		$query = 'SELECT a.alias, a.updated_at, c.alias AS cat_alias FROM article a LEFT JOIN article_category c ON a.category_id = c.id';
		$st = $con->execute($query);
		$arts = $st->fetchAll();

		foreach($arts as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('article_view', array('alias'=>$item['alias'], 'categoryalias'=>$item['cat_alias']))), substr($item['updated_at'], 0, 10), 'yearly', 1);

			
		// ADD BLOGS
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('blogs/index')), date("Y-m-d", time()), 'daily', 0.5);

		$query = 'SELECT b.id, b.updated_at FROM blog_post b';
		$st = $con->execute($query);
		$blogs = $st->fetchAll();

		foreach($blogs as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('blogs_view', array('id'=>$item['id']))), substr($item['updated_at'], 0, 10), 'yearly', 1);
			
		// ADD RELEASES
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('reliz/index')), date("Y-m-d", time()), 'daily', 0.5);

		$query = 'SELECT r.alias, r.updated_at, c.alias AS cat_alias FROM reliz r LEFT JOIN reliz_category c ON r.category_id = c.id';
		$st = $con->execute($query);
		$rels = $st->fetchAll();

		foreach($rels as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('reliz_view', array('alias'=>$item['alias'], 'categoryalias'=>$item['cat_alias']))), substr($item['updated_at'], 0, 10), 'yearly', 0.8);

		// ADD EVENTS
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('event/index')), date("Y-m-d", time()), 'daily', 0.5);

		$query = 'SELECT e.id FROM event e';
		$st = $con->execute($query);
		$evs = $st->fetchAll();

		foreach($evs as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('event_view', array('id'=>$item['id'])), NULL, 0, 10), NULL, 'yearly', 0.8);

		// ADD TENDERS
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('tender/index')), date("Y-m-d", time()), 'daily', 0.5);

		$query = 'SELECT t.id FROM tender t';
		$st = $con->execute($query);
		$tend = $st->fetchAll();

		foreach($tend as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('tender_view', array('id'=>$item['id'])), NULL , 0, 10), NULL, 'yearly', 0.8);


		// ADD CATALOG
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('catalog/index')), date("Y-m-d", time()), 'daily', 0.5);

		$query = 'SELECT c.id FROM category c';
		$st = $con->execute($query);
		$cats = $st->fetchAll();

		// ALL CATEGORIES IN ALL REGIONS
		foreach($cats as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('catalog/index').'?catid='.$item['id'].'&sort=ASC'.'&r_id=', NULL , 0, 10), NULL, 'yearly', 0.5);

		// ADD PRODUCTS
		$query = 'SELECT p.id FROM product p';
		$st = $con->execute($query);
		$cats = $st->fetchAll();

		// ALL REGIONS
		foreach($cats as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('catalog_showrpoduct', array('id' => $item['id'])), NULL , 0, 10), NULL, 'yearly', 0.5);

		// ALL STATIC PAGES
		$query = 'SELECT p.alias FROM page p';
		$st = $con->execute($query);
		$cats = $st->fetchAll();

		foreach($cats as $item)
		$this->generatePageXml($outel->addChild('url',''), $this->url_trim( url_for('static_page', array('alias' => $item['alias'])) , NULL , 0, 10), NULL, 'yearly', 0.5);

		$f = fopen($this->webdir.DIRECTORY_SEPARATOR.self::filename ,'w+');
		fwrite($f, $outel->asXml() );

		//		fwrite($f, preg_replace('[version="1.0"]' , 'version="1.0" encode="UTF-8" ', $outel->asXml()) );
		fclose($f);

		$this->matchusersdate();
	}

	protected function matchusersdate()
	{
		$users = Doctrine::getTable('sfGuardUser')->findAll();
		foreach($users as $item)
		{
			$date_past = $item->getCatalogShowTo() < date( 'Y-m-d G:i:s' ,time());
			
			switch($item->getBalance())
			{
				case 1 : if ($date_past) $item->setBalance(NULL) ; break;
				case -1: if(!$date_past) $item->setBalance(1) ; break;
				default: break;
			}
			$item->save();
		}
	}

	protected function url_trim($url)
	{
		return self::iplife.preg_replace('[\S*symfony]','',$url);
	}

	protected function generatePageXML($node, $loc = NULL, $lastmod = NULL, $changefreq = NULL, $priority = 0.5)
	{
		if($loc)
		{
			$node->addChild('loc',$loc);

			if($lastmod)
			$node->addChild('lastmod', $lastmod);

			if($priority)
			$node->addChild('priority', $priority);

		}

		/*if($changefreq)
		 $node->addChild('changefreq', $changefreq);*/
	}
}